<?php
/**
 * Beamr helper class.
 **/
class Beamr
{
  public static function getLatex($presentation, $slides = NULL) {
    // we need a context to load the partial helper 
    $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false);
    sfContext::createInstance($configuration);
    $configuration->loadHelpers('Partial');

    // récupération du header et du footer LaTeX du document.
    $presentationContext = array();
    $presentationContext['theme']           = $presentation->getBeamrThemes()->getLatexname();
    $presentationContext['colortheme']      = $presentation->getBeamrColorThemes()->getLatexname();
    $presentationContext['title']           = $presentation->getTitle();
    $presentationContext['title_short']     = $presentation->getTitleShort();
    $presentationContext['author']          = $presentation->getAuthor();
    $presentationContext['author_short']    = $presentation->getAuthorShort();
    $presentationContext['date']            = $presentation->getDate();
    $presentationContext['date_short']      = $presentation->getDateShort();
    $presentationContext['institute']       = $presentation->getInstitute();
    $presentationContext['institute_short'] = $presentation->getInstituteShort();
    $header = get_partial('presentation/latex_header', $presentationContext);
    $footer = get_partial('presentation/latex_footer', $presentationContext);

    $slides_latex = '';
    // if $slides is null, then get all slides.
    if (is_null($slides)) {
      $slides = $presentation->getBeamrSlides();
    }

    // récupération du LaTeX de tous les slides.
    foreach($slides as $slide) {
      $slidesContext = array();
      $slidesContext['plain'] = FALSE;
      $slidesContext['title'] = $slide->getTitle();
      $slidesContext['subtitle'] = $slide->getSubtitle();
      $slidesContext['content'] = $slide->getContent();
      $slidesContext['plain'] = $slide->getIsPlain();
      $slides_latex .= get_partial('slide/latex_frame', array('slide' => $slide));
    }

    return htmlspecialchars_decode($header.$slides_latex.$footer, ENT_QUOTES);
  }

  public static function makeLatex($presentation, $slides = NULL) {
    $latex = Beamr::getLatex($presentation, $slides);

    // création d'un fichier temporaire avec le LaTeX.
    $filename = tempnam('/tmp', 'beamr_tex');
    $fp  = fopen ($filename .'.tex', 'w');
    if (!$fp) {
      die ('Could not open '. $filename);
    }
    fwrite($fp, $latex);
    fclose($fp);

    ob_start();
    passthru('cd /tmp; pdflatex -output-directory /tmp -interaction nonstopmode '. $filename .'.tex 2>&1');
    if (file_exists($filename .'.log')) {
      $log = file_get_contents($filename .'.log');
      if (strpos($log, 'Fatal error')) {
        unlink($filename .'.pdf');
        // rerun, if Bookmarksfile (.out) exists
      }
      elseif (file_exists($filename .'.out') || strpos($log, 'Rerun LaTeX')) {
        passthru('cd /tmp; pdflatex -interaction nonstopmode '. $filename .'.tex 2>&1');
      }
    }
    ob_end_clean();

    return (file_exists($filename .'.pdf')) ? $filename : FALSE;
  }

  public static function makeThumbs($pdfFile, $presentation, $slides = NULL) {
    $error = FALSE;
    // if $slides is null, then get all slides.
    if (is_null($slides)) {
      $slides = $presentation->getBeamrSlides();
    }
    foreach ($slides as $i => $slide) {

      $thumbFilename = $presentation->getToken() .'_'. $slide->getId() .'.png';
      $thumbPath = sfConfig::get('app_thumbnails_path') .'/'. $thumbFilename;
      $width = sfConfig::get('app_thumbnails_width');
      passthru('convert '. $pdfFile .'['. $i .'] -quality 100 -scale '. $width .' '. $thumbPath);
      if (!file_exists($thumbPath)) {
        $error = TRUE;
      }
      else {
        $slide->setThumbnail($thumbFilename);
        $slide->save(NULL, FALSE);
      }
    }

    return !$error;
  }

  public static function clean($filename) {
    unlink($filename);
    exec ("rm $filename*");
  }
} // END class Beamr